package com.vm.activity;

import org.json.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.db.LocalUserInfoDao;
import com.vm.model.LocalUserInfo;
import com.vm.model.UserInfo;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月12日 下午6:34:00]
 * @Version: [v1.0]
 * 
 */
public class LoginActivity extends Activity {

	static final String TAG = "LoginActivity";
	EditText et_account_name;
	EditText et_account_password;
	Button btn_account_login;
	private RequestQueue mQueue;
	private LocalUserInfoDao luidao = null;
	ImageView imgbtn_search_back;

	public static Handler mainHandler;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		luidao = new LocalUserInfoDao(LoginActivity.this);

		mQueue = Volley.newRequestQueue(this);
		init();

	}

	public void init() {
		et_account_name = (EditText) findViewById(R.id.et_account_name);
		et_account_password = (EditText) findViewById(R.id.et_account_password);

		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);

		btn_account_login = (Button) findViewById(R.id.btn_account_login);

		btn_account_login.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String username = et_account_name.getText().toString();
				String password = et_account_password.getText().toString();

				if (!username.equals("") && !password.equals("")) {

					String url = VMConst.LOGINREQUEST + "?username=" + username
							+ "&password=" + password;

					JsonObjectRequest jsonObjectRequest;
					try {
						jsonObjectRequest = new JsonObjectRequest(url, null,
								new Listener<JSONObject>() {

									@Override
									public void onResponse(JSONObject arg0) {

										UserInfo userInfo = JsonUtils
												.jsonToBean(arg0,
														UserInfo.class);
										if (userInfo != null) {
											try {
												Toast toast = Toast.makeText(
														LoginActivity.this,
														"登录成功",
														Toast.LENGTH_SHORT);
												toast.show();

												LocalUserInfo localUserInfo = new LocalUserInfo();
												localUserInfo
														.setUserId(userInfo
																.getId());
												localUserInfo
														.setUsername(userInfo
																.getName());
												localUserInfo.setIntergral(userInfo
														.getIntegrity());
												localUserInfo
														.setImageUrl(userInfo
																.getIcon());
												luidao.insert(localUserInfo);

												CurrentUser.USERID = userInfo
														.getId();
												CurrentUser.USERNAME = userInfo
														.getName();
												if (userInfo.getIntegrity() != null) {
													CurrentUser.INTERGRAL = userInfo
															.getIntegrity();
												}
												if (userInfo.getIcon() != null) {
													CurrentUser.IMAGEURL = userInfo
															.getIcon();
												}

//												Intent intent = new Intent(
//														LoginActivity.this,
//														PersonCenterActivity.class);
//												LoginActivity.this
//														.startActivity(intent);

												Message message = new Message();
												message.what = 100;
												mainHandler
														.sendMessage(message);
												onBackPressed();
												LoginActivity.this.finish();
												
											} catch (Exception e) {
												// TODO Auto-generated catch
												// block
												Log.e(TAG, e.getMessage());
											}
										}
									}
								}, new Response.ErrorListener() {
									@Override
									public void onErrorResponse(
											VolleyError error) {
										Toast toast = Toast.makeText(
												LoginActivity.this, "用户名或密码错误",
												Toast.LENGTH_SHORT);
										toast.show();
									}

								});
						mQueue.add(jsonObjectRequest);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Log.e(TAG, e.getMessage());
					}

				} else {
					Toast toast = Toast.makeText(LoginActivity.this,
							"用户名或密码不能为空", Toast.LENGTH_SHORT);
					toast.show();
				}

			}
		});
	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

}
